#!/bin/bash
## initialization begin from here... v.0.01.01-2013

#netresult1=`sudo mii-tool | grep -i ok | grep -i eth0 | wc -l` ### Old Local connectivity test method
netresult1=`/sbin/ifconfig eth0 | grep UP | wc -l` 
netresult2=`ping -i 0.3 -c 5 www.af.mil | grep -i icmp_req | wc -l`
loggingfile="$PWD/jolly$(date +%d.%m.%Y).log"

#httping -s -g http://www.jollytur.com/gemi-turgruplari.html -I "is everything OK? $(date +%d.%m.%y\ %H:%M)" -R "www.testerE.net" -c 2  -i 3 -Gb
#webpage=`grep -ie sarigerme -ie fethiye -ie dalaman $htmlfile | wc -l`
#htmlfile="$workdir/jollytur.html"
#logfile="$workdir/jollytur.log"

Tomorrow=$(echo "/Jollytur$(date -d tomorrow +%d.%m.%Y).log")
WhatTimeIsIt=`date +%H%M`

htmlfile="./jollytur.html"
logfile="./jollytur.log"
balance=2
today=$(echo "/Jollytur$(date +%d.%m.%Y).log")
mailrecipients="jinhitman@gmail.com"
workdir="/home/$USER/JollyController"

touch $loggingfile

## initialization ended

control(){

	httpresult1=`httping -s -g http://www.jollytur.com -I "is everything OK? $(date +%d.%m.%y\ %H:%M)" -R "www.testerE.net" -c 2  -i 3 -Gb | grep 200\ OK | wc -l`
	sleep 1
	httpresult1=$[ $httpresult1 * 3 ] # same as => httpresult1=`echo $httpresult1 * 3 | bc`

	httpresult2=`httping -s -g http://www.jollytur.com/gemi-turgruplari.html -I "is everything OK? $(date +%d.%m.%y\ %H:%M)" -R "www.testerE.net" -c 2  -i 3 -Gb | grep 200\ OK | wc -l`
	sleep 1

	httpresult2=$[ $httpresult2 * 2 ] # same as => httpresult2=`echo $httpresult2 * 2 | bc`
		
	/usr/bin/wget -b -t 4 www.jollytur.com -O $htmlfile -o $logfile &> /dev/null
	sleep 3 
	webpage=`grep -ie sarigerme -ie fethiye -ie dalaman $htmlfile | wc -l`
	
	total=$( echo "$httpresult1 + $httpresult2" | bc)
}


clean(){
	rm -f $htmlfile $logfile

}


toFile(){

		if [ -f $workdir$today ]; then
			fileaccess=`echo "$(cat $workdir$today | wc -l ) % 15" | bc`
		
			if [ $fileaccess -eq 0 ]; then
				echo -e "HTTP1\t HTTP2\t WGET\t TIME" >> "$workdir$today"
				echo -e "$httpresult1\t $httpresult2\t $webpage\t $(date +%H:%M)" >> "$workdir$today"
			else
				echo -e "$httpresult1\t $httpresult2\t $webpage\t $(date +%H:%M)" >> "$workdir$today"
			fi
		else 
			touch $workdir$today
			echo -e "HTTP1\t HTTP2\t WGET\t TIME" >> "$workdir$today"
			echo -e "$httpresult1\t $httpresult2\t $webpage\t $(date +%H:%M)" >> "$workdir$today"
		fi

}


main(){
#############################################################################
######################   	Main Function	   ##########################
#############################################################################
###				last 3 min. Averages in calculating

if [ -f $workdir$today ]; then
SUM1=0
SUM2=0
SUM3=0

	FirstPage=`tail -6 $workdir$today | awk {'print $1'} | grep -v HTTP | tail -3` # there must be 3 x 6
	for i in $FirstPage
	do
		SUM1=$[ $i + $SUM1 ]
		echo "$SUM1"
	done

	SecondPage=`tail -6 $workdir$today | awk {'print $2'} | grep -v HTTP | tail -3` # there must be 3 x 4
	for i in $SecondPage
	do
		SUM2=$[ $i + $SUM2 ]
		echo "$SUM2"
	done
	
	WgetPage=`tail -6 $workdir$today | awk {'print $3'} | grep -v HTTP | tail -3` # there must be 3 x 4
	for i in $WgetPage
	do
		SUM3=$[ $i + $SUM3 ]
		echo "$SUM3"
	done
	
fi

	if [ $SUM1 -lt 12 ] || [ $SUM2 -lt 8 ] || [ $SUM3 -le 4 ]; then
		echo "There is something goes WRONG on jollytur.com webpage!!!" >> $loggingfile
		tail $workdir$today | grep -v HTTP > $workdir/test1 
		cat $workdir/test* | echo # msmtp sistem@bntpro.com

	fi
}


clean

if [ $netresult1 -eq 1 ]; then

        echo -e "$netresult1   &   $netresult2 \n"      
        case "$netresult2" in

                0) echo -e " Warning! There is a problem on network infrastructure ping-reply has returned nothing! $(date +%d.%m.%Y_%H:%M) " >> $loggingfile
                   echo -e " Warning! There is a problem on network infrastructure ping-reply has returned nothing! $(date +%d.%m.%Y_%H:%M) "
                exit 12
                ;;
                1) echo -e " There is soething goes wrong on Network infrastructure. We should send an e-mail to someone, who can fix that! "
                ;;
                2) echo -e " Warning! Ping-Echo Replies =>>  2   $(date +%d.%m.%Y_%H:%M) " >> $loggingfile

			control

			if [ $WhatTimeIsIt -eq 2359 ]; then 
				touch $workdir$Tomorrow
				echo -e "HTTP1\t HTTP2\t WGET\t TIME" >> "$workdir$Tomorrow"
				echo -e "$httpresult1\t $httpresult2\t $webpage\t $(date +%H:%M)" >> "$workdir$Tomorrow"
			fi
			
			toFile
			main
			exit 3
                ;;
                3) echo -e " Warning! Ping-Echo Replies =>>  3   $(date +%d.%m.%Y_%H:%M) " >> $loggingfile

			control

			if [ $WhatTimeIsIt -eq 2359 ]; then 
				touch $workdir$Tomorrow
				echo -e "HTTP1\t HTTP2\t WGET\t TIME" >> "$workdir$Tomorrow"
				echo -e "$httpresult1\t $httpresult2\t $webpage\t $(date +%H:%M)" >> "$workdir$Tomorrow"
			fi
			
			toFile
			main
			exit 2
                ;;
                4) echo -e " Warning! Ping-Echo Replies =>>  4   $(date +%d.%m.%Y_%H:%M) " >> $loggingfile

			control

			if [ $WhatTimeIsIt -eq 2359 ]; then 
				touch $workdir$Tomorrow
				echo -e "HTTP1\t HTTP2\t WGET\t TIME" >> "$workdir$Tomorrow"
				echo -e "$httpresult1\t $httpresult2\t $webpage\t $(date +%H:%M)" >> "$workdir$Tomorrow"
			fi
			
			toFile
			main
			exit 1
                ;;
                5) echo -e " Network looks Good! $(date +%d.%m.%Y_%H:%M) " >> $loggingfile
                
			control

			if [ $WhatTimeIsIt -eq 2359 ]; then 
				touch $workdir$Tomorrow
				echo -e "HTTP1\t HTTP2\t WGET\t TIME" >> "$workdir$Tomorrow"
				echo -e "$httpresult1\t $httpresult2\t $webpage\t $(date +%H:%M)" >> "$workdir$Tomorrow"
			fi
			
			toFile
			main
			exit 0
		;;
                *) echo -e " There is a problem in CASE "

        esac

else

        echo " it seems, Eth0 link is down! "
        echo " it seems, Eth0 link is down! "  >> $loggingfile
        exit 11

fi


